// pages/suopei/suopei.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    suopei: []
  },

  suopei:function(e){
    console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: '../suopei/suopei_item/suopei_item?id='+e.currentTarget.dataset.id,
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    let tableName = "suopei"

    // 应用查询对象
    let Product = new wx.BaaS.TableObject(tableName)

    // 不设置查询条件
    Product.find().then(res => {
      // success

      console.log(res.data.objects)

      var items = []
      var myDate = new Date();
      var year = myDate.getFullYear();
      var mouth = myDate.getMonth()+1;
      var day = myDate.getDate();
      var i = 0;
      console.log(year + '.' + mouth + '.' + day)
      var A = this.qsort(res.data.objects , 0 , res.data.objects.length )
      console.log(A)
      while (i < A.length) {
       
        
        var item = {
          shijian: year + '.' + mouth + '.' + day,
          bg: [],
          tab: [],
          bgc: [],
          tab2: [],
          ids:[],
          contents: []          
        }
        console.log(i)
        for (var j = i ; j < A.length ; j++) {
          var a = Math.floor(Math.random() * 5)
          var c = Math.floor(Math.random() * 5)
          var b = Math.floor(Math.random() * 4)
          if (A[i].shijian == item.shijian) {
            item.contents.push(res.data.objects[i])
            item.bg.push(app.globalData.color[a])
            item.tab.push(app.globalData.tab[b])
            item.bgc.push(app.globalData.color[c])
            item.tab2.push(app.globalData.tab[b])
            item.ids.push(j)
            i++;
          }
        }
        day = day - 1;
        if(item.contents.length != 0){
          items.push(item)
        }
        
      }
      this.setData({
        item:items
      })
      app.globalData.suopei = A
    }, err => {
      // err
    })
  },
  swap(A, i, j) {
    const t = A[i];
    A[i] = A[j];
    A[j] = t;
  },
  
  /**
   *
   * @param {*} A  数组
   * @param {*} p  起始下标
   * @param {*} r  结束下标 + 1
   */
  divide(A, p, r) {
    const x = A[r - 1].shijian.split(".")[2];
    let i = p - 1;
  
    for (let j = p; j < r - 1; j++) {
      if (A[j].shijian.split(".")[2] >= x) {
        i++;
        this.swap(A, i, j);
      }
    }
  
    this.swap(A, i + 1, r - 1);
  
    return i + 1;
  },
  
  /**
   * 
   * @param {*} A  数组
   * @param {*} p  起始下标
   * @param {*} r  结束下标 + 1
   */
  qsort(A, p = 0, r) {
    r = r || A.length;
  
    if (p < r - 1) {
      const q = this.divide(A, p, r);
      this.qsort(A, p, q);
      this.qsort(A, q + 1, r);
    }
  
    return A;
  },


})